﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Huluwa;

[RequireComponent(typeof(Rigidbody2D))]
public class Danmaku : MonoBehaviour
{

    public static int count = 0;
    private TextMesh _textMesh;
    private Rigidbody2D _rigidbody;

    public GameObject platformObj;
    public float paddingWidth = 0.2f;
    public float paddingHeight = 0.1f;
    public DanmakuType attitude{get;private set;}
    public string sortingLayerName = "Danmaku";

    public GameObject harmfulObj;
    public GameObject helpfulObj;
    
    
    
    
    // Start is called before the first frame update
    void Awake()
    {
        _textMesh = GetComponentInChildren<TextMesh>();
        _rigidbody = GetComponent<Rigidbody2D>();
        _rigidbody.simulated = false;
    }

    private void Start()
    {
        attitude = DanmakuType.neutral;
        MeshRenderer meshRenderer = GetComponentInChildren<MeshRenderer>();
        if (meshRenderer)
        {
            meshRenderer.sortingLayerName = sortingLayerName;
            meshRenderer.sortingOrder = count;
            
        }
        SpriteRenderer spriteRenderer = GetComponentInChildren<SpriteRenderer>();
        if (spriteRenderer)
        {
            spriteRenderer.sortingLayerName = sortingLayerName;
            spriteRenderer.sortingOrder = count;
            
        }
        count++;
    }
    public bool IsInView(Vector3 worldPos)
    {
        Vector3 cameraPos = Camera.main.transform.position;
        Vector3 distance = worldPos - cameraPos;
        float offsetX = Mathf.Abs(distance.x);
        float offsetY = Mathf.Abs(distance.y);
        // Camera.main.;
        float size = 4 * Camera.main.orthographicSize;
        if (offsetX < size && offsetY < size){
            return true;
        }
        else
            return false;
    }

    private void Update()
    {
        if(!IsInView(this.transform.position)){
            GlobalConfig.Instance.danmakuCount--;
            _rigidbody.Sleep();
            // Destroy(_rigidbody);
            Destroy(gameObject);
            // Destroy(this);
        }
    }
    
    public void SetData(Message data){
        attitude = data.atti;
       
    }
    public void SetText(string newText)
    {
        if(newText.Length > 200){
            newText = newText.Substring(0,200);
        }
        if(_textMesh) _textMesh.text =  newText;
        Bounds bound = _textMesh.GetComponent<Renderer>().bounds;    
        EdgeCollider2D edgeCollider2D = platformObj.GetComponent<EdgeCollider2D>();
        edgeCollider2D.points = new Vector2[]
        {
            new Vector2(-0.5f * bound.size.x, 0.5f * bound.size.y),
            new Vector2(0.5f * bound.size.x, 0.5f * bound.size.y)
        };
            
        SpriteRenderer sr = platformObj.GetComponent<SpriteRenderer>();
        sr.size = new Vector3(
            bound.size.x + paddingWidth,
            bound.size.y + paddingHeight);
        
        
    }


    public float GetWidth()
    {
        Bounds bound = _textMesh.GetComponent<Renderer>().bounds;
        return bound.size.x;
    }
    
    public void SetVelocity(Vector2 vel)
    {
        _rigidbody.velocity = vel;
    }

    public void SetPosition(Vector2 pos)
    {
        transform.position = pos;
        _rigidbody.simulated = true;
    }

}
